<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input
        v-model="listQuery.keyword"
        size="small"
        placeholder="请输入用户名"
        clearable
        class="filter-item w-200"
      />

      <el-input
        v-model="listQuery.user_phone"
        size="small"
        placeholder="请输入手机"
        clearable
        class="filter-item w-200"
      />
      <el-button-group class="filter-item">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-refresh"
          @click="refresh"
        >
          重置
        </el-button>

      </el-button-group>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      height="100%"
      class="table-container"
      highlight-current-row
    >
      <el-table-column
        label="ID"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column
        label="麻辣社区uid"
        width="140"
        align="center"
      >
        <template slot-scope="scope">
          <a :href="'https://www.mala.cn/home.php?mod=space&uid='+scope.row.mala_id" target="_blank" style="color:#409EFF;display: flex;align-items: center;justify-content: center" v-if="scope.row.mala_id>0"><img src="https://wap.mala.cn/mala/imgs/114.png" style="width:16px;margin-right:5px;">{{ scope.row.mala_id }}</a>
        </template>
      </el-table-column>

      <el-table-column
        label="用户名"
        width="250"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.user_name||scope.row.user_realname }}
        </template>
      </el-table-column>

      <el-table-column
        label="注册来源"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.user_comefrom }}
        </template>
      </el-table-column>


      <el-table-column
        width="130"
        label="手机"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-phone" round size="mini" v-if="scope.row.user_phone=='-'" @click="lookPhone('user',scope.row.id,scope.$index)">
            查看手机</el-button>
          <span v-else>{{scope.row.user_phone}}</span>
        </template>
      </el-table-column>




      <el-table-column
        label="注册时间"
        width="160"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="230"
        align="center"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="success"
              icon="el-icon-s-custom"
              size="mini"
              @click="login(scope.row.mala_id,scope)"
            >
              登录
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-user"
              size="mini"
              @click="gouser(scope.row.id)"
            >
              Ta的留言
            </el-button>

          </el-button-group>

        </template>
      </el-table-column>


    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />


  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList,getPhone } from '@/api/user'
import { deepClone } from '@/utils'

const _temp = {
  id: '',
  username: '',
  truename: '',
  role: [],
  type: '管理员',
  email: '',
  status: 1
}

export default {
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        created_at: undefined,
        status: undefined,
        keyword: undefined,
        user:1
      },
      temp: Object.assign({}, _temp),
      dialogVisible: false,
      dialogType: 'create',
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    lookPhone(type,id,key){
      getPhone({type:type,id:id}).then(r=>{
        this.$set(this.list[key],'user_phone',r.data)
      })
    },
    login(uid){
      this.$prompt('请输入你的登录密码', '授权提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        if(!value){
          this.$message({
            type: 'error',
            message: '请输入密码'
          });
          return false
        }
        // this.$message({
        //   type: 'success',
        //   message: '你的邮箱是: ' + value
        // });
      }).catch(() => {

      });
    },
    gouser(id){
      this.$router.push({ path: '/wz/list', query: { uid: id } })
    },
    search() {
      this.fetchData()
    },
    refresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        created_at: undefined,
        status: undefined,
        keyword: undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = Object.assign({}, _temp)
    },
    add() {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit(scope) {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'modify'
      this.temp = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    changeStatus(value, scope) {
      setTimeout(() => {
        this.list[scope.$index].status = value
        this.$message({
          message: '更新成功',
          type: 'success'
        })
      }, 300)
    },
    del(scope) {
      this.$confirm('确认删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        setTimeout(() => {
          this.list.splice(scope.$index, 1)
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        }, 300)
      })
    },
    submit() {
      if (this.loading) {
        return
      }
      this.loading = true
      setTimeout(() => {
        this.$message({
          message: '提交成功',
          type: 'success'
        })
        this.dialogVisible = false
        this.loading = false
      }, 300)
    }
  }
}
</script>
